/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.xuexiang.xui_lib.component.textview.supertextview.adapter;

import ohos.agp.components.Attr;
import ohos.agp.components.AttrSet;
import ohos.agp.components.element.Element;
import ohos.agp.utils.Color;
import ohos.app.Context;

import com.xuexiang.xui_lib.util.AttrValue;

/**
 * TypedArray移植AttrSet适配器
 *
 * @since 2021-04-08
 */
public class TypedArray {
    private static final EmptyAttr EMPTY_ATTR = new EmptyAttr();
    private final AttrSet attrs;

    /**
     * 构造器
     *
     * @param attrs AttrSet数据集合
     */
    public TypedArray(AttrSet attrs) {
        this.attrs = attrs;
    }

    /**
     * 返回attr对应的Color值
     *
     * @param attr attrName
     * @param defValue 默认值
     * @return attr对应的值或默认值
     */
    public int getColor(String attr, int defValue) {
        return AttrValue.get(attrs, attr, defValue);
    }

    /**
     * 返回attr对应的Color值
     *
     * @param attr attrName
     * @param defValue 默认值
     * @return attr对应的值或默认值
     */
    public Color getColor(String attr, Color defValue) {
        return AttrValue.get(attrs, attr, defValue);
    }

    /**
     * 返回attr对应的Int值
     *
     * @param attr attrName
     * @param defValue 默认值
     * @return attr对应的值或默认值
     */
    public int getInt(String attr, int defValue) {
        return AttrValue.get(attrs, attr, defValue);
    }

    /**
     * 返回attr对应的Dimension值
     *
     * @param attr attrName
     * @param defValue 默认值
     * @return attr对应的值或默认值
     */
    public int getDimensionPixelSize(String attr, int defValue) {
        return AttrValue.getDimension(attrs, attr, defValue);
    }

    /**
     * 返回attr对应的Boolean值
     *
     * @param attr attrName
     * @param defValue 默认值
     * @return attr对应的值或默认值
     */
    public boolean getBoolean(String attr, boolean defValue) {
        return AttrValue.get(attrs, attr, defValue);
    }

    /**
     * 返回attr对应的值
     *
     * @param attr attrName
     * @param defValue 默认值
     * @param <T> 默认值类型
     * @return attr对应的值或默认值
     */
    public <T> T get(String attr, T defValue) {
        return AttrValue.get(attrs, attr, defValue);
    }

    /**
     * 返回attr对应的String值, 如果没有则返回null
     *
     * @param attr attrName
     * @return String类型值
     */
    public String getString(String attr) {
        return attrs.getAttr(attr).orElse(EMPTY_ATTR).getStringValue();
    }

    /**
     * 获取绑定的颜色
     *
     * @param attr attrName
     * @return 绑定的颜色
     */
    public Color getColorStateList(String attr) {
        return getColor(attr, null);
    }

    /**
     * 返回attr对应的Element
     *
     * @param attr key
     * @return key对应的Element
     */
    public Element getElementAttrRes(String attr) {
        return attrs.getAttr(attr).orElse(EMPTY_ATTR).getElement();
    }

    /**
     * 空Attr实现接口
     *
     * @since 2021-04-08
     */
    private static final class EmptyAttr implements Attr {
        @Override
        public String getName() {
            return null;
        }

        @Override
        public String getStringValue() {
            return null;
        }

        @Override
        public int getIntegerValue() {
            return 0;
        }

        @Override
        public boolean getBoolValue() {
            return false;
        }

        @Override
        public float getFloatValue() {
            return 0;
        }

        @Override
        public long getLongValue() {
            return 0;
        }

        @Override
        public Element getElement() {
            return null;
        }

        @Override
        public int getDimensionValue() {
            return 0;
        }

        @Override
        public Color getColorValue() {
            return null;
        }

        @Override
        public Context getContext() {
            return null;
        }
    }
}
